package com.engine.modules.general.controller;

import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.exception.InvalidTokenRuntimeException;
import com.engine.common.util.TokenVerifyUtil.AuthRedisUtil;
import com.engine.common.util.TokenVerifyUtil.TokenVerifyUtil;
import com.engine.modules.general.mapper.GeneralDataCommonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "5-常规数据通用接口")
@CrossOrigin
@RequestMapping("/GeneralData/Common")
@ApiSort(value = 5)
@Slf4j
public class GeneralDataCommonController {

    @Autowired
    GeneralDataCommonMapper generalDataCommonMapper;
    @Autowired
    AuthRedisUtil authRedisUtil;

    @ApiOperation(value = "通过where子句查询数据", notes = "通过where子句查询数据，返回数据全部字段的json")
    @GetMapping(value = "/selectDataByWhereStr/{tableName}/{token}")
    public String selectDataByWhereStr(@PathVariable("tableName") String tableName,
                                       @RequestParam("whereStr") String whereStr,
                                       @PathVariable("token") String token,
                                       HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            List<Map<String, Object>> rsList = generalDataCommonMapper.selectDataByWhereStr(tableName, whereStr, access);
            if (rsList.size() > 0) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList, 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (InvalidTokenRuntimeException e) {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }
    @ApiOperation(value = "获取表信息", notes = "通过表名查询表信息，会返回json格式的表信息")
    @GetMapping(value = "/selectTableInfo/{tableName}/{token}", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectTableInfo(@PathVariable("tableName") String tableName,
                                  @PathVariable("token") String token,
                                  HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            Map<String, Object> map = generalDataCommonMapper.selectTableInfo(tableName);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", map, 1, response);
            return jsonResponseEntity.getJsonString();
        } catch (InvalidTokenRuntimeException e) {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }
    @ApiOperation(value = "执行数据统计", notes = "通过统计名或者表名执行存储在sys_table_statistical_properties的统计sql,并将结果返回为json")
    @GetMapping(value = "/selectStatisticalData/{TableName}/{token}")
    public String selectStatisticalData(@PathVariable("TableName") String TableName,
                                        @PathVariable("token") String token,
                                        @RequestParam("statisticalNames") String statisticalNames,
                                        HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            if (statisticalNames == "") {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，统计名不能为空", response);
                return jsonResponseEntity.getJsonString();
            }
            List<Map<String, Object>> rsList = generalDataCommonMapper.selectStatisticalData(statisticalNames, access);
            if (rsList.size() > 0) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList, 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (InvalidTokenRuntimeException e) {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "执行数据统计", notes = "通过统计名和geometry或者表名执行存储在sys_table_statistical_properties的统计sql,并将结果返回为json")
    @PostMapping(value = "/selectStatisticalDataByGeometry/{TableName}/{token}")
    public String selectStatisticalDataByGeometry(@PathVariable("TableName") String TableName,
                                                  @PathVariable("token") String token,
                                                  @RequestParam("statisticalNames") String statisticalNames,
                                                  @RequestBody String geometry,
                                                  HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            if (TableName == "" || statisticalNames == "") {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，统计名和表名不能为空", response);
                return jsonResponseEntity.getJsonString();
            }
            List<Map<String, Object>> rsList = generalDataCommonMapper.selectStatisticalDataByGeometry(statisticalNames, geometry, access);
            if (rsList.size() > 0) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList, 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果或无权限", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (InvalidTokenRuntimeException e) {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }




}
